﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Tzync
{
    public partial class ResolveConflictUIForm : Form
    {
        private SyncTool syncTool;
        private List<Difference> conflictList;  //list of conflicts found
        private List<Difference> resolvedConflictList;

        public ResolveConflictUIForm(SyncTool newSyncTool, List<Difference> newConflictList)
        {
            this.syncTool = newSyncTool;
            this.conflictList = newConflictList;
            this.resolvedConflictList = new List<Difference>();

            InitializeComponent();
        }

        private void ResolveConflictUI_Load(object sender, EventArgs e)
        {
            applyBtn.Enabled = false;
            conflictComboBox.SelectedIndex = 0; //defaults
            applyToolTip.SetToolTip(applyBtn, "Click here to apply the chosen sync direction to selected files");
            dropdownToolTip.SetToolTip(conflictComboBox, "Click here to choose a sync direction for your selected files");
            DisplayConflicts();
        }

        //displays conflicts found in UI
        public void DisplayConflicts()
        {
            TreeNode tNode = new TreeNode();
            ListViewItem items = new ListViewItem();
            Type typeOfConflict;
            Pair<DirectionType, OperationType> conflictDetails;
            bool toAdd = false;

            foreach (Difference conflictFound in conflictList)
            {
                typeOfConflict = conflictFound.GetItemType();
                conflictDetails = conflictFound.GetPairType();
                toAdd = DisplayConflict(items, typeOfConflict, conflictDetails, tNode, conflictFound);
                
                if (toAdd == true)
                    conflictListView.Items.Add(items); 
            }
        }

        private bool DisplayConflict(ListViewItem items, Type typeOfConflict, Pair<DirectionType, OperationType> conflictDetails, TreeNode tNode, Difference conflictFound)
        {
            if (typeOfConflict == Type.File)
            {
                FileInfo fileInfo = conflictFound.GetSysFile();
                tNode = conflictFound.GetPartnerFile();

                if (syncTool.GetSystemName() == SystemType.Host)
                {
                    items.Text = tNode.getFullPathA(); //path of file/folder  
                    items.SubItems.Add("" + tNode.getSyncA());  //last sync time of file/folder
                }
                else if (syncTool.GetSystemName() == SystemType.Guest)
                {
                    items.Text = tNode.getFullPathB(); //path of file/folder  
                    items.SubItems.Add("" + tNode.getSyncB());  //last sync time of file/folder
                }

                if (conflictDetails.Second == OperationType.CreationCreationConflict)
                {
                    items.SubItems.Add("Conflict - file created on both computers");
                }
                else if (conflictDetails.Second == OperationType.ModDeleteConflict)
                {
                    items.SubItems.Add("Conflict - file modified on 1 computer and deleted on the other");
                }
                else if (conflictDetails.Second == OperationType.ModModConflict)
                {
                    items.SubItems.Add("Conflict - file modified on both computers");
                }
                else if (conflictDetails.Second == OperationType.DeletionDeletionConflict)
                {
                    items.SubItems.Add("Conflict - file deleted on both computers");
                }

                items.SubItems.Add("To be resolved");     //direction of operation
                items.ForeColor = Color.Red;

                return true;
            }

            else if (typeOfConflict == Type.Folder)
            {
                //ignore del-del, mod-mod and creation-creation folder conflicts
                if (conflictDetails.Second != OperationType.DeletionDeletionConflict
                && conflictDetails.Second != OperationType.ModModConflict && conflictDetails.Second != OperationType.CreationCreationConflict)
                {
                    DirectoryInfo dirInfo = conflictFound.GetSysFolder();
                    tNode = conflictFound.GetPartnerFolder();

                    if (syncTool.GetSystemName() == SystemType.Host)
                    {
                        items.Text = tNode.getFullPathA(); //path of file/folder  
                        items.SubItems.Add("" + tNode.getSyncA());  //last sync time of file/folder
                    }
                    else if (syncTool.GetSystemName() == SystemType.Guest)
                    {
                        items.Text = tNode.getFullPathB(); //path of file/folder  
                        items.SubItems.Add("" + tNode.getSyncB());  //last sync time of file/folder
                    }

                    if (conflictDetails.Second == OperationType.ModDeleteConflict)
                    {
                        items.SubItems.Add("Conflict - folder modified on 1 computer and deleted on the other");
                    }

                    else if (conflictDetails.Second == OperationType.CreationCreationConflict)
                    {
                        items.SubItems.Add("Conflict - folder created on both computers");
                    }

                    if (conflictDetails.Second == OperationType.DeletionDeletionConflict)
                    {
                        items.SubItems.Add("Conflict - folder deleted on both computers");
                    }

                    items.SubItems.Add("To be resolved");     //direction of operation
                    items.ForeColor = Color.Red;

                    return true;
                }
            }

            return false;
        }

        private void conflictListView_ItemChecked(object sender, ItemCheckedEventArgs e)
        {
            if (e.Item.Checked == true)
            {
                applyBtn.Enabled = true;
            }
        }

        private void applyBtn_Click(object sender, EventArgs e)
        {
            Difference conflictFound;
            List<Difference> resolvedConflictList = new List<Difference>();

            foreach (ListViewItem item in conflictListView.CheckedItems)
            {
                conflictFound = conflictList[item.Index];
                if (conflictComboBox.SelectedIndex == 0)
                {
                    conflictFound.SetPairType(new Pair<DirectionType, OperationType>(DirectionType.SystemToHD, conflictFound.GetPairType().Second));
                }
                else if (conflictComboBox.SelectedIndex == 1)
                {
                    conflictFound.SetPairType(new Pair<DirectionType, OperationType>(DirectionType.HDToSystem, conflictFound.GetPairType().Second));
                }
                
                resolvedConflictList.Add(conflictFound);
                conflictList.RemoveAt(item.Index);
                conflictListView.Items.Remove(item);
   
            }
            
            syncTool.SetResolvedDifferenceList(resolvedConflictList);
            syncTool.ResolveConflicts(resolvedConflictList);

            DialogResult resolveResult = MessageBox.Show("Selected conflict(s) have been resolved!",
                        "Hurray!", MessageBoxButtons.OK, MessageBoxIcon.Information);

            if (resolveResult == DialogResult.OK)
            {
                if (conflictList.Count == 0)
                {
                    this.Close();
                }
                else
                {
                    return;
                }
            }
            else
                return;
        }

        private void ResolveConflictUIForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (conflictList.Count > 0)
            {
                MessageBox.Show("Please resolve all conflicts before exiting!",
                        "Sorry!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                e.Cancel = true;
            }
        }
    }
}
